var app = {};

/**
 * Bot class
 */
app.Bot = {
	// Flag to check if bot enabled
	active : true,
	// Current page
	currentPage : null,
	// Is debug mode enabled
	debugEnabled : false,
	// Last value of timer
	lastTimerValue : '',
	
	// Bot stats
	stats : {
		// Main stats
		levels : 0,			// Number of level gained
		win : 0,				// Wins
		loose : 0,			// Looses
		quests : 0,			// Quests done
		dungeons : 0,	// Dungeons completed
		towers : 0,		// Towers completed
		// Player attributes
		attr : {
			health : 0,
			attack : 0,
			defence : 0,
			accuracy : 0,
			evasion : 0,
			luck : 0
		},
		// Loot
		loot : {
			// Main loot
			gold : 0,
			platinum : 0,
			diamonds : 0,
			shards : 0,
			credits : 0,
			// Keys
			keys : {
				green : 0,
				blue : 0,
				yellow : 0,
				purple : 0,
				red : 0
			},
			// Tower keys
			towerkeys : {
				green : 0,
				blue : 0,
				yellow : 0,
				purple : 0,
				red : 0
			},
			// Runes
			runes : {
				// Minor runes found
				minor : {
					gray : 0,
					green : 0,
					blue : 0,
					yellow : 0,
					purple : 0,
					red : 0
				},
				// Major runes found
				major : {
					gray : 0,
					green : 0,
					blue : 0,
					yellow : 0,
					purple : 0,
					red : 0
				},
				// Anciend runes found
				ancient : {
					gray : 0,
					green : 0,
					blue : 0,
					yellow : 0,
					purple : 0,
					red : 0
				}
			}
		}
	},
	
	/**
	 * Bot initialization
	 */
	init : function() {
		var that = this;
		
		this.$content = $('#contentmain');
		this.$timer = $('#actiontimer');
		
		return this;
	},
	
	/**
	 * Start the Bot
	 */
	start : function() {
		this.active = true;
		
		this.definePage();
		
		this.checkTimer();
		
		return this;
	},
	
	/**
	 * Stop the Bot
	 */
	stop : function() {
		this.active = false;
		
		return this;
	},
	
	/**
	 * Check the timer value and do some actions
	 * If changed from "Waiting..." to something else, then page is loaded and we need to define what is the current page
	 * If changed to "0.0" then page is ready for actions
	 */
	checkTimer : function() {
		var newValue = this.$timer.text(),
			that = this;
		
		if (!this.active) {
			return;
		}
		
		if (this.lastTimerValue != newValue) {
			// New page is loaded
			if (this.lastTimerValue == 'Waiting...') {
				this.definePage();
			}
			
			// Page is ready to do next actions
			if (newValue == '0.0' && this.currentPage) {
				this.debug('Ready for action');
				
				this.currentPage.action();
			}
			
			this.lastTimerValue = newValue;
		}
		
		setTimeout(function() {
			that.checkTimer();
		}, 100);
	},
	
	/**
	 * Define the current page
	 */
	definePage : function() {
		var pages = ['main', 'recaptcha', 'fight', 'tradeskill', 'quests'];
		
		this.currentPage = null;
		
		for (var i = 0; i < pages.length; i++) {
			if (app.Page[pages[i]].is()) {
				this.debug('New page');
			
				this.currentPage = app.Page[pages[i]];
				this.currentPage.init();
				
				return true;
			}
		}
	},
	
	/**
	 * Add new record to log
	 *
	 * @param text Message to log
	 */
	debug : function(text) {
		if (this.debugEnabled) {
			console.log(text);
		}
		
		return this;
	}
};